home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / macgzip_03b2-src / macos / think / MacGzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-18  |  11.7 KB  |  578 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include <AppleEvents.h>
  7. #include <GestaltEqu.h>
  8. #include <Sound.h>
  9.  
  10. #include <string.h>
  11.  
  12. #include "MacGzip.h"
  13. #include "ThePrefs.h"
  14. #include "MacErrors.h"
  15. #include "SPDProg.h"
  16. #include "MacAE.h"
  17.  
  18. void    InitTheMac(void);
  19. void    main(void);
  20. void    MainEvent(void);
  21. void    DoCommand(long mResult);
  22. void    DoMouseDown(short, WindowPtr, EventRecord *);
  23. OSErr    DoOpen( FSSpec *myFSSpec );
  24. long    RecurseDir(long dirIDToSearch);
  25.  
  26. void    CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect);
  27. void    about(void);
  28. static    pascal char MyFilter ( DialogPtr, EventRecord*, short* );
  29. void    SetUpMenus(void);
  30. void    MyBeep(void);
  31.  
  32. extern    OSErr OpenFile( Str255 fName);
  33.  
  34.  
  35. /* Globals */
  36.  
  37. Boolean            quitting = false;
  38.  
  39. MenuHandle        myMenus[4];
  40. OSErr            theOSErr;
  41. KeyMap            theKeys;
  42. char            charKey;
  43. Boolean            modKey;
  44. HFileInfo        gpb;
  45.  
  46.  
  47. /***************************************************************************/
  48.  
  49. void InitTheMac(void)
  50. {
  51.     long    gestAliasResponse,
  52.             gestStdFileResponse,
  53.             gestFSResponse,
  54.             gestAEResponse;
  55.  
  56.     FlushEvents(everyEvent, 0);
  57.     InitGraf(&qd.thePort);
  58.     InitFonts();
  59.     InitWindows();
  60.     InitMenus();
  61.     TEInit();
  62.     InitDialogs(nil);
  63.     InitCursor();
  64.     MaxApplZone();
  65.     
  66. /* sys 7 ?*/
  67.  
  68.     theOSErr = Gestalt(gestaltAppleEventsAttr, &gestAEResponse);
  69.     theOSErr = Gestalt(gestaltAliasMgrAttr, &gestAliasResponse);
  70.     if (theOSErr == noErr) theOSErr = Gestalt(gestaltFSAttr, &gestFSResponse);
  71.     if (theOSErr == noErr)
  72.         theOSErr = Gestalt(gestaltStandardFileAttr, &gestStdFileResponse);
  73.     
  74.     if (theOSErr != noErr ||
  75.         (gestAliasResponse & (1 << gestaltAliasMgrPresent)) == 0 ||
  76.         (gestFSResponse & (1 << gestaltHasFSSpecCalls)) == 0 ||
  77.         (gestStdFileResponse & (1 << gestaltStandardFile58)) == 0 ||
  78.         ((gestAEResponse >> gestaltAppleEventsPresent) & 0x0001) == 0
  79.         )
  80.     {
  81.     
  82.         theAlert( OLD_SYSTEM,GENERIC, theOSErr, true );
  83.     }
  84.     
  85.     CanBreak=true;
  86.  
  87.     if (!GetOrGeneratePrefs(&currPrefs, kCurrentPrefsTypeVers))
  88.     {
  89.         about();
  90.         prefsChangedFlag = DoPrefsDialog(&currPrefs);
  91.         if (prefsChangedFlag)
  92.         {
  93.             if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  94.                     theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  95.         }
  96.         else ExitToShell();
  97.     }
  98.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  99.                                                     MyHandleODoc,0, FALSE);
  100.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  101.                                                     MyHandleOApp,0, FALSE);
  102.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  103.                                                     MyHandlePDoc,0, FALSE);
  104.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  105.                                                     MyHandleQuit,0, FALSE);
  106.  
  107. }
  108.  
  109. void main( void )
  110. {
  111.     extern    Boolean        gSavedPos;
  112.     extern    Point        gSavedPoint;
  113.     
  114.     extern Handle    FPrefs;
  115.     extern Size        FPrefsSize;
  116.  
  117.  
  118.     InitTheMac();
  119.     SetUpMenus();
  120.         
  121.     while( !quitting )
  122.     {
  123.         MainEvent();
  124.     }
  125.  
  126. /* remember window pos */
  127.             
  128.     currPrefs.SavedPos = gSavedPos; /* always true */
  129.         
  130.     if(( gSavedPoint.h != currPrefs.SavedPoint.h ) ||
  131.         ( gSavedPoint.v != currPrefs.SavedPoint.v ) )
  132.     {
  133.         currPrefs.SavedPoint.h = gSavedPoint.h;
  134.         currPrefs.SavedPoint.v = gSavedPoint.v;
  135.         prefsChangedFlag=true;
  136.     }
  137.  
  138.     if (prefsChangedFlag)
  139.     {
  140.         if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  141.                 theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  142.     }
  143.     
  144.     if( FPrefsSize != 0 ) DisposeHandle( FPrefs );
  145.     if( ICmappings != nil ) DisposeHandle( ICmappings );
  146.     
  147.     ExitToShell();
  148. }
  149.  
  150.  
  151. void MainEvent(void)
  152. {
  153.     EventRecord myEvent;
  154.     WindowPtr whichWindow;
  155.     short windowPart;
  156.  
  157.     if ( WaitNextEvent ( everyEvent, &myEvent, 0, nil ) ) {
  158.         switch( myEvent.what ) {
  159.             case mouseDown:
  160.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  161.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  162.                 break;
  163.                 
  164.             case keyDown:
  165.             case autoKey: 
  166.             {
  167.                 register char theChar;
  168.     
  169.                 theChar = myEvent.message & charCodeMask;
  170.                 if ((myEvent.modifiers & cmdKey) != 0) 
  171.                     DoCommand( MenuKey(theChar) );
  172.                                     
  173.                 break;
  174.             }
  175.                 
  176.             case activateEvt:
  177.                 break;
  178.  
  179.             case updateEvt: 
  180.                 break;
  181.                 
  182.             case diskEvt:
  183.             if ((myEvent.message >> 16) != noErr)
  184.             {
  185.                 Point myPoint={80,80};
  186.                 
  187.                 theOSErr = DIBadMount(myPoint, myEvent.message);
  188.                 theAlert(BAD_DISK,GENERIC,theOSErr,false);
  189.             }
  190.             break;
  191.  
  192.             case osEvt:
  193.             switch ((myEvent.message >> 24) & 0x0ff)
  194.             {
  195.                 case suspendResumeMessage:
  196.                     
  197.                     SetCursor(&qd.arrow);
  198.                 
  199.                     if ((myEvent.message & resumeFlag) == 0) // suspend 
  200.                     {
  201.                     }
  202.                     else
  203.                     {                                        // resume
  204.                     }
  205.                     break;
  206.                     
  207.                 case mouseMovedMessage:
  208.                     break;
  209.             }
  210.             break;
  211.                 
  212.             case kHighLevelEvent:
  213.                 AEProcessAppleEvent (&myEvent);
  214.                 break;
  215.  
  216.  
  217.         }
  218.     }
  219. }
  220. void DoCommand(long mResult)
  221. {
  222.     short    theItem;
  223.     Str255    name;
  224.  
  225.     theItem = LoWord(mResult);
  226.     switch( HiWord(mResult) ) {
  227.         case appleID:
  228.             if( theItem > 2 ) {
  229.                 GrafPtr savePort;
  230.                 GetItem(myMenus[appleM], theItem, name);
  231.                 GetPort(&savePort);
  232.                 OpenDeskAcc(name);
  233.                 SetPort(savePort);
  234.             }
  235.             else
  236.             {
  237.                 about();
  238.             }
  239.             break;
  240.  
  241.         case fileID:
  242.         
  243.             switch( theItem )
  244.             {
  245.                 case fmOpen:
  246.                 
  247.                     if ((GzipOK==DoOpen( (FSSpec *)nil )) &&
  248.                         (currPrefs.BeepWhenDone))
  249.                     MyBeep();
  250.  
  251.  
  252.                     break;
  253.         
  254.                 case fmPrefs:
  255.                 
  256.                     prefsChangedFlag = DoPrefsDialog(&currPrefs);
  257.                     if (prefsChangedFlag)
  258.                     {
  259.                         if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  260.                                 theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  261.                     }
  262.                     break;
  263.         
  264.                 case fmQuit:
  265.                 
  266.                     quitting = true;
  267.                     break;
  268.             }
  269.             break;
  270.  
  271.         case editID:
  272.         
  273.             SystemEdit(theItem-1);
  274.             break;
  275.             
  276.         case gzipID:
  277.         
  278.             switch(theItem){
  279.             
  280.             case gmAscii:
  281.             
  282.                 currPrefs.ascii=true;
  283.                 CheckItem( myMenus[gzipM], gmBin, false );
  284.                 CheckItem( myMenus[gzipM], gmAscii, true);
  285.                 break;
  286.  
  287.             case gmBin:
  288.             
  289.                 currPrefs.ascii=false;
  290.                 CheckItem( myMenus[gzipM], gmAscii , false );
  291.                 CheckItem( myMenus[gzipM], gmBin, true);
  292.                 break;
  293.             
  294.             case gmComp:
  295.             
  296.                 currPrefs.compress=true;
  297.                 CheckItem( myMenus[gzipM], gmUncomp, false );
  298.                 CheckItem( myMenus[gzipM], gmComp, true);
  299.                 break;
  300.  
  301.             case gmUncomp:
  302.             
  303.                 currPrefs.compress=false;
  304.                 CheckItem( myMenus[gzipM], gmComp , false );
  305.                 CheckItem( myMenus[gzipM], gmUncomp, true);
  306.                 break;
  307.             
  308.             }
  309.             prefsChangedFlag = true;
  310.             break;
  311.     }
  312.  
  313.     HiliteMenu(0);
  314. }
  315.  
  316. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  317. {
  318.     switch( windowPart ) {
  319.         case inGoAway:
  320.             break;
  321.  
  322.         case inMenuBar:
  323.             DoCommand( MenuSelect(myEvent->where) );
  324.             break;
  325.  
  326.         case inSysWindow:
  327.             SystemClick(myEvent, whichWindow);
  328.             break;
  329.  
  330.         case inDrag:
  331.             break;
  332.  
  333.         case inGrow:
  334.             break;
  335.  
  336.         case inContent:
  337.             if( whichWindow != FrontWindow() )
  338.                 SelectWindow(whichWindow);
  339.             break;
  340.     }
  341. }
  342.  
  343. /***************************************************************************
  344.  *
  345.  * We use OpenWD and SetVol in order to use fopen on stdio programs
  346.  * DoOpen(nil) prompts for a file
  347.  * RecurseDir() is recursive (what happends to the stack?)
  348.  */
  349.  
  350.  
  351. OSErr DoOpen( FSSpec *myFSSpec )
  352. {
  353.     StandardFileReply    mySFR;
  354.     SFTypeList            typeList={GZIP_ID,'ZIVU','pZIP','ZIVM'};
  355.     HFileInfo            pb;
  356.     OSErr                error,
  357.                         result;
  358.     short                myVRefNum,
  359.                         SaveVRefNum;
  360.     
  361.     if ( myFSSpec == nil )
  362.     {
  363.         StandardGetFile( nil, -1, nil, &mySFR );
  364. /*        StandardGetFile( nil,  4, typeList, &mySFR );*/
  365.  
  366.         if( !mySFR.sfGood ) return 1;
  367.         
  368.         myFSSpec = &(mySFR.sfFile);
  369.         
  370.         charKey = 0x00;
  371.         modKey = false;
  372.     }
  373.     else
  374.     {
  375.         modKey        =    (( IsOptKey(theKeys) ) ? true : false);
  376.         
  377.         if (IsAKey(theKeys))
  378.                                     charKey = 'a';
  379.         else if (IsBKey(theKeys))
  380.                                     charKey = 'b';
  381.         else
  382.                                     charKey = 0x00;
  383.     }
  384.  
  385.     pb.ioNamePtr    = myFSSpec->name;
  386.     pb.ioVRefNum    = myFSSpec->vRefNum;
  387.     pb.ioFDirIndex    = 0;                    /* query 1 item */
  388.     pb.ioDirID        = myFSSpec->parID;
  389.     
  390.     error = PBGetCatInfo( (CInfoPBPtr)&pb, 0 );
  391.     if (((pb.ioFlAttrib>>4) & 0x01) == 1)
  392.     {
  393.         /* Is a Dir */
  394.         gpb.ioNamePtr = myFSSpec->name;
  395.         gpb.ioVRefNum = myFSSpec->vRefNum;
  396.  
  397.         if ( RecurseDir(pb.ioDirID) > 0 )
  398.             result = GzipOK;
  399.     }
  400.     else
  401.     {
  402.         /* Is a File */
  403.         OpenWD(myFSSpec->vRefNum,myFSSpec->parID,GZIP_ID, &myVRefNum );
  404.         error=GetVol( nil, &SaveVRefNum );        
  405.         error=SetVol( nil, myVRefNum);
  406.         
  407.             result = OpenFile( myFSSpec->name );
  408.     
  409.         error=SetVol( nil, SaveVRefNum );
  410.         CloseWD(myVRefNum );
  411.     }
  412.     
  413.     if    (
  414.         ( (StartupFiles) || ( result == GzipOK ) ) &&
  415.         ( currPrefs.QuitWhenDone )
  416.         )
  417.         quitting=true;
  418.         
  419.     
  420.     return result;
  421. }
  422.  
  423. long RecurseDir(long dirIDToSearch)
  424. {
  425.     short int    index=1;      /* for ioFDirIndex */
  426.     OSErr        err;
  427.     short        myVRefNum,
  428.                 SaveVRefNum;
  429.     long        NumOfFiles;
  430.     
  431.                         
  432.     OpenWD(gpb.ioVRefNum,dirIDToSearch,GZIP_ID, &myVRefNum );
  433.     err=GetVol( nil, &SaveVRefNum );        
  434.     err=SetVol( nil, myVRefNum);
  435.     NumOfFiles=0;
  436.     do
  437.     {
  438.          gpb.ioDirID        = dirIDToSearch;     
  439.          gpb.ioFDirIndex    = index;        /* set up the index */
  440.                                             /* we need to do this every time through,
  441.                                              * since GetCatInfo returns ioFlNum
  442.                                              * in this field
  443.                                              */
  444.  
  445.         err= PBGetCatInfo((CInfoPBPtr)&gpb,false);
  446.         if (err == noErr) 
  447.         {
  448.             /* check the file attributes for folderhood */
  449.             if (((gpb.ioFlAttrib>>4) & 0x01) == 1)
  450.              {
  451.                 NumOfFiles += RecurseDir(gpb.ioDirID);
  452.                 err = 0;
  453.             }
  454.             else 
  455.             {
  456.                 if (GzipOK==OpenFile( gpb.ioNamePtr ))
  457.                     NumOfFiles++ ;
  458.             }
  459.             index += 1;    /* increment the index for GetCatInfo */
  460.         }
  461.     } while (err == noErr);
  462.     
  463.     err=SetVol( nil, SaveVRefNum );
  464.     CloseWD(myVRefNum );
  465.     
  466.     return(NumOfFiles);
  467. }
  468.  
  469.  
  470. /***************************************************************************
  471.  *
  472.  * About, Beep and other things
  473.  */
  474.  
  475. void CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect)
  476. {
  477.     short        dh,dv;
  478.     
  479.     *inRect = (*thePic)->picFrame;
  480.     dh = (outRect->right - (inRect->right - inRect->left)) / 2;
  481.     dv = (outRect->bottom - (inRect->bottom - inRect->top)) / 2;
  482.     OffsetRect(inRect,dh,dv);
  483. }
  484.  
  485. void   about( void )
  486. {
  487.     DialogPtr    GetSelection;              
  488.     short        itemHit;                       
  489.     short        DType; 
  490.     Handle        DItem;
  491.     GrafPtr        savePort;
  492.  
  493.     Rect        picRect;
  494.     PicHandle    thePict;
  495.  
  496.     GetSelection = GetNewDialog(dlogAbout, nil, (WindowPtr)-1);
  497.     ShowWindow(GetSelection);           
  498.     SelectWindow(GetSelection);         
  499.     GetPort(&savePort);
  500.     SetPort(GetSelection);
  501.  
  502.  
  503.     GetDItem(GetSelection, 1, &DType, &DItem, &picRect);
  504.     thePict = (PicHandle) Get1Resource('PICT',ABOUT);
  505.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  506.     DrawPicture(thePict,&picRect);
  507.  
  508. /*    WARNING (?)
  509.  *    Should be
  510.  *    ModalDialog(NewModalFilterProc((ProcPtr)MyFilter), &itemHit);
  511.  *    for PPC ?
  512.  */
  513. #if defined(__MWERKS__)
  514.     ModalDialog(NewModalFilterProc((ProcPtr)MyFilter), &itemHit);
  515. #else
  516.     ModalDialog( (ProcPtr) MyFilter, &itemHit);
  517. #endif
  518.     
  519.  
  520.     EraseRect (&GetSelection->portRect);
  521.     ReleaseResource((Handle)thePict);
  522.     
  523.     thePict = (PicHandle) Get1Resource('PICT',GPL);
  524.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  525.     DrawPicture(thePict,&picRect);
  526.     
  527. #if defined(__MWERKS__)
  528.     ModalDialog(NewModalFilterProc((ProcPtr)MyFilter), &itemHit);
  529. #else
  530.     ModalDialog( (ProcPtr) MyFilter, &itemHit);
  531. #endif
  532.     ReleaseResource((Handle)thePict);
  533.  
  534.     DisposDialog(GetSelection);  
  535.     SetPort(savePort);
  536.     
  537. }                                       
  538. static pascal char
  539.             MyFilter (
  540.                         DialogPtr        theDialog,      
  541.                         EventRecord       *theEvent,    
  542.                         short            *itemHit
  543.             )
  544. {
  545.     return ((theEvent->what == keyDown)||(theEvent->what == mouseDown));
  546. }
  547. /***************************************************************************/
  548. void SetUpMenus(void)
  549. {
  550.     short index;
  551.     
  552.     ClearMenuBar();
  553.     
  554.     myMenus[appleM] = GetMenu(appleID);
  555.     AddResMenu(myMenus[appleM], 'DRVR');
  556.     myMenus[fileM] = GetMenu(fileID);
  557.     myMenus[editM] = GetMenu(editID);
  558.     myMenus[gzipM] = GetMenu(gzipID);
  559.     for(index=appleM; index <= gzipM; index++)
  560.         InsertMenu(myMenus[index], 0) ;
  561.  
  562.     CheckItem( myMenus[gzipM], (currPrefs.ascii?gmAscii:gmBin), true);
  563.     CheckItem( myMenus[gzipM], (currPrefs.compress?gmComp:gmUncomp), true);
  564.     
  565.     DrawMenuBar();
  566. }
  567. /***************************************************************************/
  568. void MyBeep(void)
  569. {
  570.     Handle theSound;
  571.     
  572.         theSound = GetResource('snd ', SOUND_ID);
  573.             if (theSound != nil)
  574.             {
  575.                 SndPlay(nil, theSound, false);
  576.                 ReleaseResource(theSound);
  577.             }
  578. }